from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_restful import Api
from flask_cors import CORS
from config import Config

db = SQLAlchemy()
login_manager = LoginManager()
api = Api()

def create_app():
    app = Flask(__name__)
    app.config.from_object(Config)
    
    # 添加CORS支持
    CORS(app, resources={r"/*": {
        "origins": ["http://localhost:8080", "http://localhost:8081"],
        "methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
        "allow_headers": ["Content-Type", "Authorization"],
        "supports_credentials": True
    }})

    db.init_app(app)
    login_manager.init_app(app)
    api.init_app(app)

    from app.routes import auth, workflow
    app.register_blueprint(auth.bp)
    app.register_blueprint(workflow.bp)

    return app 